home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-api-22.lha / AmiTCP-2.2 / src / netlib / gethostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-12  |  2.2 KB  |  85 lines

  1. RCS_ID_C="$Id: gethostname.c,v 1.3 1993/06/03 22:59:38 ppessi Exp $";
  2. /*
  3.  * gethostname.c -- get host name from the environment variable "HOSTNAME"
  4.  *
  5.  * Author: jraja <Jarno.Rajahalme@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Tue Apr 27 17:49:15 1993 jraja
  12.  * Last modified: Fri Jun  4 01:58:48 1993 ppessi
  13.  *
  14.  */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <errno.h>
  19.  
  20. /****** net.lib/gethostname ******************************************
  21. *
  22. *   NAME   
  23. *       gethostname -- get the name of the host
  24. *
  25. *   SYNOPSIS
  26. *       error = gethostname(name, namelen);
  27. *
  28. *       int gethostname(char *, int);
  29. *
  30. *   FUNCTION
  31. *       Get the name of the host to the buffer name of length namelen.
  32. *       The name is taken from the environment variable "HOSTNAME"
  33. *       where it SHOULD reside.
  34. *
  35. *   INPUTS
  36. *       name    - Pointer to the buffer where the name should be
  37. *                 stored.
  38. *       namelen - Length of the buffer name.
  39. *
  40. *   RESULT
  41. *       error   - 0 on success, -1 in case of an error. The global
  42. *                 variable errno will be set to indicate the error as
  43. *                 follows: 
  44. *
  45. *                 ENOENT - The environment variable "HOSTNAME" is not
  46. *                          found.
  47. *  
  48. *   EXAMPLE
  49. *       char hostname[MAXHOSTNAMELEN];
  50. *       int error;
  51. *       
  52. *       error = gethostname(hostname, sizeof(hostname));
  53. *       if (error < 0)
  54. *         exit(10);
  55. *       
  56. *       printf("My name is \"%s\".\n", hostname);
  57. *
  58. *   NOTES
  59. *       This function is included for source compatibility with Unix
  60. *       systems.
  61. *       The ENOENT errno value is AmiTCP/IP addition.
  62. *
  63. *   BUGS
  64. *       Unlike the Unix version, this version assures that the
  65. *       resulting string is always NULL-terminated.
  66. *
  67. *   SEE ALSO
  68. *       getenv()
  69. *****************************************************************************
  70. *
  71. */
  72.  
  73. int gethostname(char *name, int namelen)
  74. {
  75.   char *cp = getenv("HOSTNAME");
  76.  
  77.   if (cp == NULL) {
  78.     errno = ENOENT;
  79.     return -1;
  80.   }
  81.   stccpy(name, cp, namelen);
  82.   free(cp);
  83.   return 0;
  84. }
  85.